package com.kaka.service.serviceimpl;

import com.kaka.mapper.PermissionMapper;
import com.kaka.mapper.RoleMapper;
import com.kaka.mapper.UsersMapper;
import com.kaka.mapper.UsersRoleMapper;
import com.kaka.pojo.Permission;
import com.kaka.pojo.Role;
import com.kaka.pojo.Users;
import com.kaka.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("usersService")
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UsersRoleMapper usersRoleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    //如果没有查询到对应数据 返回null(最好手动抛出异常)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询数据返回实体类对应数据
        Users users = usersMapper.selectByUserName(username);
        if (users != null) {
            User user = new User(users.getUsername(),
                    users.getPassword(),
                    users.getStatus() != 0,
                    true, true, true,
                    getAuthority(users.getId()));
            return user;
        } else {
            throw new UsernameNotFoundException("账号不存在");
        }
    }

    //默认users中没有任何权限 无法登录
    public Set<GrantedAuthority> getAuthority(int uid) {
        Set<GrantedAuthority> simpleGrantedAuthorities = new HashSet<>();
        List<Permission> permissionList = permissionMapper.selectByUid(uid);
        if (permissionList != null){
            for (Permission permission : permissionList) {
                simpleGrantedAuthorities.add(new SimpleGrantedAuthority(permission.getPermissionName()));
            }
        }
        return simpleGrantedAuthorities;
    }


    @Override
    public List<Users> findAll() {
        return usersMapper.selectAll();
    }

    @Override
    public Users findById(int id) {
        return usersMapper.selectByUid(id);
    }

    @Transactional
    @Override
    public int edit(Users users, int roleId) {
        users.setPassword(passwordEncoder.encode(users.getPassword()));
        int i = usersMapper.updateUsers(users);
        int j = usersRoleMapper.deleteByUid(users.getId());
        int k = usersRoleMapper.insertMessage(users.getId(), roleId);
        return i + j + k;
    }

    @Override
    public List<Role> findAllRole() {
        return roleMapper.selectAll();
    }

    @Transactional
    @Override
    public int deleteUsers(int id) {
        int i = usersMapper.deleteById(id);
        int j = usersRoleMapper.deleteByUid(id);
        return i + j;
    }

    @Transactional
    @Override
    public int addUsers(Users users, int roleId) {
        users.setPassword(passwordEncoder.encode(users.getPassword()));
        int i = usersMapper.insertUsers(users);
        int j = usersRoleMapper.insertMessage(users.getId(), roleId);
        return i + j;
    }

    public Users findByUsersName(String username) {
        return usersMapper.selectByUserName(username);
    }
}